package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.EvaluateData.EvaluateList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import java.util.Random;
import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 评论列表
 */
public class EvaluateListAdapter extends RecyclerView.Adapter<EvaluateListAdapter.MyViewHolder> {


    private Context context;
    private List<EvaluateList> evaluateLists;
    private Random random;

    public EvaluateListAdapter(Context context, List<EvaluateList> evaluateLists) {
        this.context = context;
        this.evaluateLists = evaluateLists;
        random = new Random();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.evaluste_list_item, null);

        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        EvaluateList evaluate = evaluateLists.get(position);
//        Picasso.with(context).load(evaluate.getUserpic()).
//                placeholder(R.mipmap.user).
//                error(R.mipmap.user)
//                .into(holder.civUserImage);


        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.user)
                .showImageForEmptyUri(R.mipmap.user)
                .showImageOnFail(R.mipmap.user)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();

        ImageLoader.getInstance().displayImage(evaluate.getUserpic(),holder.civUserImage,options);

        //评论日期
        holder.tvEvaluateDate.setText(evaluate.getPostTime());
        //评论内容
        holder.tvEvaluateContent.setText(evaluate.getComment());

        int num = 0;
        String n = "";
        for (int i = 0; i < 3; i++) {
            num = random.nextInt(10);
            n = n + String.valueOf(num);
        }


        //随机手机号
        holder.tvEvaluateUserPhone.setText("183******" + n);

    }

    @Override
    public int getItemCount() {
        return evaluateLists == null ? 0 : evaluateLists.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.civ_user_image)
        CircleImageView civUserImage;
        @Bind(R.id.tv_evaluate_user_phone)
        TextView tvEvaluateUserPhone;
        @Bind(R.id.tv_evaluate_content)
        TextView tvEvaluateContent;
        @Bind(R.id.tv_evaluate_date)
        TextView tvEvaluateDate;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
